<!--
 * @Author: 小花
 * @Date: 2022-05-22 12:19:58
 * @LastEditors: 小花
 * @LastEditTime: 2022-05-26 23:30:50
 * @FilePath: /permission_client/src/views/MenuTypeView.vue
 * @Description: 菜单类型管理
-->
<template>
  <el-table :data="menuTypeList" style="width: 100%">
    <el-table-column label="菜单ID" prop="id" />
    <el-table-column label="菜单类型" prop="type" />
    <el-table-column label="菜单名称" prop="name" />
    <el-table-column>
      <template #header>
        <el-button @click="dialogFormVisible = true">添加菜单类型</el-button>
      </template>
      <template #default="scope">
        <el-button @click="execEditMenuType(scope.row)">Edit</el-button>
        <el-button type="danger" @click="execDelMenuType(scope.row)">Delete</el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-dialog v-model="dialogFormVisible" title="添加菜单类型">
    <el-form :model="form">
      <el-form-item label="菜单类型">
        <el-input v-model="form.type" autocomplete="off" />
      </el-form-item>
      <el-form-item label="菜单名称">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">Cancel</el-button>
        <el-button type="primary" @click="execaddMenuType">Confirm</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang='ts'>
import { defineComponent, reactive, toRefs } from 'vue'
import { addMenuType, delMenuType, pullAllMenuType } from '@/request'
import { responseEnum, responseUtil } from '@/request/responseUtil'
import MenuTypeView, { IMenuType } from '@/model/MenuTypeView'
import { ElMessage } from 'element-plus'

export default defineComponent({
  name: 'MenuTypeView',
  setup () {
    const coreData = reactive(new MenuTypeView())

    const execpullAllMenuType = async () => {
      const result = await (await pullAllMenuType()).data as responseUtil<IMenuType[]>
      if (result.code === responseEnum.OK) {
        coreData.menuTypeList = result.data || []
        if (!result.data.length) ElMessage.warning('暂无数据')
      }
    }
    execpullAllMenuType()

    const execaddMenuType = async () => {
      const result = await (await addMenuType(coreData.form)).data as responseUtil
      if (result.code === responseEnum.OK) {
        coreData.menuTypeList.push(result.data)
        coreData.dialogFormVisible = false
        ElMessage.success(result.msg)
      } else if (result.code === responseEnum.FAIL) {
        ElMessage.error(result.msg)
      }
    }

    const execDelMenuType = async (row: IMenuType) => {
      const result = await (await delMenuType([row.id])).data as responseUtil
      if (result.code === responseEnum.OK) {
        coreData.menuTypeList.splice(coreData.menuTypeList.findIndex(item => item.id === row.id), 1)
        ElMessage.success(result.msg)
      } else if (result.code === responseEnum.FAIL) {
        ElMessage.error(result.msg)
      }
    }

    const execEditMenuType = async (row: IMenuType) => {
      console.log('功能待开发', row)
      // const result = await (await editMenuType()).data as responseUtil
    }

    return {
      ...toRefs(coreData),
      execaddMenuType,
      execDelMenuType,
      execEditMenuType
    }
  }
})
</script>

<style lang='scss' scoped>
</style>
